package com.woniu.microprojectserver.personmag.controller;

import com.woniu.microcommonsserver.entity.BuildInfoEntity;
import com.woniu.microcommonsserver.entity.BuildPersonInfoEntity;
import com.woniu.microcommonsserver.entity.LpersonInfoEntity;
import com.woniu.microcommonsserver.entity.SpacePersonInfoEntity;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microprojectserver.bean.BuildTreeUtils;
import com.woniu.microprojectserver.bean.PostAndPerson;
import com.woniu.microprojectserver.bean.TreeData;
import com.woniu.microprojectserver.bean.WithPerson;
import com.woniu.microprojectserver.buildmag.service.impl.BuildServiceImpl;
import com.woniu.microprojectserver.personmag.service.impl.PersonServiceImpl;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/lperson")
public class PersonController {

    @Resource
    private PersonServiceImpl personService;

    @GetMapping(value = "/findByBuildId",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByBuildId(String id){
        List<LpersonInfoEntity> lists = personService.findByBuildId(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(lists).build();
    }

    @PostMapping(value = "/addByBuild",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg addByBuild(@RequestBody PostAndPerson postAndPerson){
        personService.addByBuild(postAndPerson.getLpersonInfoEntity());
        BuildPersonInfoEntity bp = new BuildPersonInfoEntity();
        bp.setFkBuildId(Long.valueOf(postAndPerson.getBid()));
        bp.setFkPersonId(postAndPerson.getLpersonInfoEntity().getId());
        boolean b = personService.addByBuildWith(bp);
        return ResponseMsg.builder().code(200).msg("添加成功").data(b).build();
    }

    @PostMapping(value = "/update",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg update(@RequestBody LpersonInfoEntity lpersonInfoEntity){
        boolean b = personService.update(lpersonInfoEntity);
        return ResponseMsg.builder().code(200).msg("修改成功").data(b).build();
    }


    @GetMapping(value = "/deleteByPersonBuild",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg deleteByPersonBuild(String id){
        boolean a = personService.deleteByPersonBuild(id);
        return ResponseMsg.builder().code(200).msg("删除成功").data(a).build();
    }

    @GetMapping(value = "/findBySpaceId",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findBySpaceId(String id){
        List<LpersonInfoEntity> lists = personService.findBySpaceId(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(lists).build();
    }

    @PostMapping(value = "/addBySpace",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg addBySpace(@RequestBody PostAndPerson postAndPerson){
        personService.addBySpace(postAndPerson.getLpersonInfoEntity());
        SpacePersonInfoEntity sp = new SpacePersonInfoEntity();
        sp.setFkSpaceId(Long.valueOf(postAndPerson.getBid()));
        sp.setFkPersonId(postAndPerson.getLpersonInfoEntity().getId());
        boolean b = personService.addBySpaceWith(sp);
        return ResponseMsg.builder().code(200).msg("添加成功").data(b).build();
    }


    @GetMapping(value = "/deleteByPersonSpace",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg deleteByPersonSpace(String id){
        boolean a = personService.deleteByPersonSpace(id);
        return ResponseMsg.builder().code(200).msg("删除成功").data(a).build();
    }

}
